package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.GeneralConfiguration;
import com.ruoyi.system.domain.HolidaySettings;
import com.ruoyi.system.service.IGeneralConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 假期设置Controller
 * 
 * @author ruoyi
 * @date 2022-09-25
 */
@RestController
@RequestMapping("/system/globalSettings")
public class GeneralConfigurationController extends BaseController
{
    @Autowired
    private IGeneralConfigurationService generalConfigurationService;

    @Value("${holidaySettingsName}")
    private String fileName;

    /**
     * 查询假期设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:globalSettings:list')")
    @GetMapping("/list")
    public TableDataInfo list(GeneralConfiguration generalConfiguration)
    {
        startPage();
        List<GeneralConfiguration> list = generalConfigurationService.selectHolidaySettingsList(generalConfiguration);
        return getDataTable(list);
    }

    //获取所有假期信息
    @PreAuthorize("@ss.hasPermi('system:globalSettings:getHolidaySettingsList')")
    @GetMapping("/getHolidaySettingsList")
    public AjaxResult getHolidaySettingsList(){
        List<GeneralConfiguration> generalConfigurations = generalConfigurationService.selectHolidaySettingsList(null);
        return AjaxResult.success(generalConfigurations);
    }

    /**
     * 导入假期设置
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "假期设置", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:globalSettings:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<GeneralConfiguration> util = new ExcelUtil<GeneralConfiguration>(GeneralConfiguration.class);
        List<GeneralConfiguration> generalConfigurationList = util.importExcel(file.getInputStream());
        generalConfigurationService.importHolidaySettings(generalConfigurationList, updateSupport);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {

        try
        {
            if (!FileUtils.checkAllowDownload(fileName))
            {
                throw new Exception(StringUtils.format("文件名称({})非法，不允许下载。 ", fileName));
            }
            String filePath = RuoYiConfig.getDownloadPath() + fileName;

            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, fileName);
            FileUtils.writeBytes(filePath, response.getOutputStream());
        }
        catch (Exception e)
        {
            throw new GlobalException("下载文件失败");
        }
    }

    /**
     * 导出假期设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:globalSettings:export')")
    @Log(title = "假期设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GeneralConfiguration generalConfiguration)
    {
        List<GeneralConfiguration> list = generalConfigurationService.selectHolidaySettingsList(generalConfiguration);
        ExcelUtil<GeneralConfiguration> util = new ExcelUtil<GeneralConfiguration>(GeneralConfiguration.class);
        util.exportExcel(response, list, "假期设置数据");
    }

    /**
     * 获取假期设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:globalSettings:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(generalConfigurationService.selectHolidaySettingsById(id));
    }

    /**
     * 新增假期设置
     */
    @PreAuthorize("@ss.hasPermi('system:globalSettings:add')")
    @Log(title = "假期设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GeneralConfiguration generalConfiguration)
    {
        return toAjax(generalConfigurationService.insertHolidaySettings(generalConfiguration));
    }

    /**
     * 修改假期设置
     */
    @PreAuthorize("@ss.hasPermi('system:globalSettings:edit')")
    @Log(title = "假期设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GeneralConfiguration generalConfiguration)
    {
        return toAjax(generalConfigurationService.updateHolidaySettings(generalConfiguration));
    }

    /**
     * 删除假期设置
     */
    @PreAuthorize("@ss.hasPermi('system:globalSettings:remove')")
    @Log(title = "假期设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(generalConfigurationService.deleteHolidaySettingsByIds(ids));
    }

    /**
     * 查询所有考勤信息
     */
    @PreAuthorize("@ss.hasPermi('system:globalSettings:attendanceList')")
    @GetMapping("/attendanceList")
    public AjaxResult attendanceList(){
        return AjaxResult.success(generalConfigurationService.selectAttendanceList());
    }

    /**
     * 保存考勤设置
     */
    @Log(title = "考勤时间", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:globalSettings:saveAttendanceTime')")
    @PostMapping("/saveAttendanceTime")
    public AjaxResult saveAttendanceTime(@RequestBody Map<String,Object> map)
    {
        return toAjax(generalConfigurationService.saveAttendanceSetting(map));
    }
}
